package answer.service.Impl;

import answer.mapper.DetailsMapper;
import answer.service.DetailsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import commons.pojo.Details;
import commons.vo.LivegoodsResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DetailsServiceImpl implements DetailsService {
    @Autowired
    private DetailsMapper detailsMapper;

    @Override
    public Details searchDetail(int username) {
        QueryWrapper<Details> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",username);
        Details details = null;
        try {
            details = detailsMapper.selectOne(objectQueryWrapper);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return details;
    }

    @Override
    public LivegoodsResult setDetails(Details details) {
        if(details.getName() == null) return LivegoodsResult.error("错误");
        QueryWrapper<Details> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("id",details.getId());
//        Details details2 = detailsMapper.selectById(details.getId());
//        if(details2 == null) return LivegoodsResult.error();
        Details details1 = detailsMapper.selectOne(objectQueryWrapper);
        if (details1 == null) {
            detailsMapper.insert(details);
            return LivegoodsResult.ok("设置成功");
        }else {
//            QueryWrapper<Details> object = new QueryWrapper<>();
//            objectQueryWrapper.eq("id",details.getId());
            detailsMapper.updateById(details);
            return LivegoodsResult.ok("设置成功");
        }
    }
}